package com.huluobo.a2204kotlinshopping.register

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.TextUtils
import android.util.Log
import com.blankj.utilcode.util.ToastUtils
import com.huluobo.a2204kotlinshopping.R
import com.huluobo.a2204kotlinshopping.databinding.ActivityRegisterBinding
import com.huluobo.base.view.BaseMVVMActivity

class RegisterActivity : BaseMVVMActivity<ActivityRegisterBinding, RegisterViewModel>() {
    override fun initData() {
        viewModel.registerSuccess.observe(this) {
            ToastUtils.showLong(it.message)
            if (it.code == 200) {
                finish()
            }
        }

        viewModel.registerFailed.observe(this) {
            Log.i("TagA", "注册失败:$it")
        }
    }

    override fun initView() {
        dataBinding.registerBtn.setOnClickListener {
            if (TextUtils.isEmpty(dataBinding.registerUsernameTv.text.toString())) {
                ToastUtils.showLong("用户名不能为空")
            } else {
                if (TextUtils.equals(
                        dataBinding.registerPasswordTv.text.toString(),
                        dataBinding.registerRepasswordTv.text.toString()
                    )
                ) {
                    viewModel.register(
                        dataBinding.registerUsernameTv.text.toString(),
                        dataBinding.registerPasswordTv.text.toString(),
                        dataBinding.registerRepasswordTv.text.toString()
                    )
                } else {
                    dataBinding.registerRepasswordTv.error = "两次密码输入不一致"
                }
            }
        }
    }

    override fun getViewModel(): Class<RegisterViewModel> {
        return RegisterViewModel::class.java
    }

    override fun getLayoutId(): Int = R.layout.activity_register
}